package cn.com.allunion.sms.service.mybatis.impl;

import cn.com.allunion.common.page.QueryResult;
import cn.com.allunion.sms.mapper.UserbaseMapper;
import cn.com.allunion.sms.pojo.Userbase;
import cn.com.allunion.sms.pojo.UserbaseExample;
import cn.com.allunion.sms.service.mybatis.UserbaseService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 短信平台管理员 业务逻辑接口 实现类
 * Created by YangJie on 2016/4/18.
 */
@Service
@Transactional
public class UserbaseServiceImpl implements UserbaseService {
    private static final Logger LOGGER = LoggerFactory.getLogger(UserbaseServiceImpl.class) ;

    @Autowired
    UserbaseMapper userbaseMapper ;

    @Override
    public boolean save(Userbase userbase) {
        if (null == userbase) {
            LOGGER.warn("the userbase object is null.");
            return false ;
        }

        int result = userbaseMapper.insert(userbase) ;

        if (result > 0) {
            return true ;
        }
        return false;
    }

    @Override
    public boolean update(Userbase userbase) {
        if (null == userbase) {
            LOGGER.warn("the userbase object is null.");
            return false ;
        }
        int result = userbaseMapper.updateByPrimaryKey(userbase) ;
        return (result > 0) ;
    }

    @Override
    public boolean deleteById(String userId) {
        if (StringUtils.isBlank(userId)) {
            LOGGER.warn("the userId object is empty.");
            return false ;
        }

        int result = userbaseMapper.deleteByPrimaryKey(userId);
        return (result > 0) ;
    }

    @Override
    @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
    public Userbase findById(String userId) {
        if (StringUtils.isBlank(userId)) {
            LOGGER.warn("the param of userId is null.");
            return null ;
        }
        return userbaseMapper.selectByPrimaryKey(userId);
    }


    @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
    @Override
    public List<Userbase> findByCriteria(String userName, String userType, Boolean enabled, Boolean nonLocked, Boolean writepermission) {
        UserbaseExample example = new UserbaseExample();
        UserbaseExample.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotBlank(userName)) {
            criteria.andUserNameEqualTo(userName) ;
        }
        if (StringUtils.isNotBlank(userType)) {
            criteria.andUserTypeEqualTo(userType);
        }
        if (null != enabled) {
            criteria.andEnabledEqualTo(enabled);
        }
        if (null != nonLocked) {
            criteria.andNonLockedEqualTo(nonLocked);
        }
        if (null != writepermission) {
            criteria.andWritepermissionEqualTo(writepermission);
        }

        return userbaseMapper.selectByExample(example) ;
    }

    @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
    @Override
    public QueryResult<Userbase> getScrollData(int pageCurrent, int pageSize) {
        UserbaseExample example = new UserbaseExample();
        QueryResult<Userbase> queryResult = null ;
        if (-1 == pageCurrent || -1 == pageSize) { // 获取全部数据
            List<Userbase> userbases = userbaseMapper.selectByExample(example) ;
            queryResult = new QueryResult<>(userbases, userbases.size()) ;
        } else { // 分页获取数据
            PageHelper.startPage(pageCurrent, pageSize) ;
            List<Userbase> userbases = userbaseMapper.selectByExample(example) ;
            PageInfo<Userbase> pageInfo = new PageInfo<>(userbases) ;
            queryResult = new QueryResult<>(userbases, pageInfo.getTotal()) ;
        }

        return queryResult;
    }

    @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
    @Override
    public Userbase login(String userName, String passWord) {
        if (StringUtils.isBlank(userName) || StringUtils.isBlank(passWord)) {
            LOGGER.warn("the userName or passWord is empty. userName: {} , passWord: {}", userName, passWord);
            return null ;
        }

        UserbaseExample example = new UserbaseExample();
        UserbaseExample.Criteria criteria = example.createCriteria();
        criteria.andUserNameEqualTo(userName);
        criteria.andPassWordEqualTo(passWord) ;

        List<Userbase> userbases = userbaseMapper.selectByExample(example);
        if (CollectionUtils.isNotEmpty(userbases)) { // 登录成功
            return userbases.get(0) ;
        }

        return null;
    }
}
